﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreGallery.Addins.Contracts;
using System.IO;
using System.Windows.Media.Imaging;
using System.ComponentModel;

namespace CoreGallery.Storage.fs
{
    public class FileSystemPhoto : CoreGallery.Addins.Contracts.IPhoto
    {
        private string _filename, _name;
        private long _width, _height;
        private Guid _id;
        private IList<MetadataElement> _metadata = new List<MetadataElement>();
        private BitmapDecoder decoder;

        #region IPhoto Members

        public FileSystemPhoto(string filename)
        {
            _filename = filename;
            decoder = BitmapDecoder.Create(new Uri(_filename), BitmapCreateOptions.DelayCreation, BitmapCacheOption.OnDemand);
            _width = (long)decoder.Frames[0].Width;
            _height = (long)decoder.Frames[0].Height;
            _metadata.Add(new MetadataElement("Title", decoder.Metadata.Title));
            _metadata.Add(new MetadataElement("Author", string.Join(",", decoder.Metadata.Author.ToArray())));
            _metadata.Add(new MetadataElement("Comment", decoder.Metadata.Comment));
            _metadata.Add(new MetadataElement("DateTaken", decoder.Metadata.DateTaken));
            _metadata.Add(new MetadataElement("Keywords", string.Join(",", decoder.Metadata.Keywords.ToArray())));
            _metadata.Add(new MetadataElement("Location", decoder.Metadata.Location));
            _metadata.Add(new MetadataElement("Rating", decoder.Metadata.Rating + " stars"));
        }

        public Guid UniqueId
        {
            get { return _id; }
        }

        public IList<MetadataElement> Metadata
        {
            get { return _metadata; }
        }

        public string Title
        {
            get { return _name; }
            set { 
                if( _name != value )
                {
                    _name = value;
                    OnPropertyChanged("Title");
                }
        }

        public long Width
        {
            get { return _width; }
        }

        public long Height
        {
            get { return _height; }
        }

        public System.IO.Stream GetPhotoStream()
        {
            return File.OpenRead(_filename);
        }

        public System.IO.Stream GetThumbnailStream()
        {
            return File.OpenRead(_filename);
        }


        public string Subject
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Description
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Creator
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Publisher
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Contributor
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public DateTime Date
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Type
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Format
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Identifier
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Relation
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Coverage
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged ( string name ) 
        { 
            PropertyChangedEventHandler ph = PropertyChanged; 
            if  ( ph != null ) 
            { 
                ph ( this, new PropertyChangedEventArgs (name)); 
            } 
        }


        #endregion
    }
}
